package srzy.module

import java.io.File
import scala.collection.immutable.Nil
import srzy.common.Logging

object CleanDir extends AbsCaseDeal with Logging {

  val dirs = cdrPaths.map(_._2)
  //  logger.debug("cdrDirMap:" + cdrDirMap.toList.sortBy(_._1).map(x => x._1 + " : " + x._2).mkString("\n"))
  //  val fList = dirs.flatMap(x => {
  //    val f = new File(x)
  //    f.listFiles().toList
  //  })

  def deal = {
    logger.debug("start clean dir")
    //    while (true) {

    //    val l2 = l.filter(x => !(x.getName() matches """[a-zA-Z0-9_.]*\.(dcfmt|r|err|cdr|tmp|trans|transerr)"""))
    //    if (l2.length != 0) {
    //      logger.error("illegal file name:" + l2.map(_.getName()).mkString(" "))
    //      throw new Exception("文件后缀错误!")
    //    }

    //    var flag = true
    //    for (i <- fList) {
    //      logger.debug("del:" + i.getName())
    //      val dr = i.delete()
    //      logger.debug("delRes:" + dr)
    //      if (dr == false) flag = false
    //    }
    //    Thread.sleep(500)

    deleteFiles

    while (remainFiles) {
      Thread.sleep(500)
      deleteFiles
    }

    //    if (flag == false) {
    //      deal
    //    }

  }

  def deleteFiles() = {
    val fileList = dirs.flatMap(f => {
      //      val f = new File(x)
      f.listFiles().toList
    }).foreach { i =>
      logger.debug("del:" + i.getName())
      val dr = i.delete()
      logger.debug("delRes:" + dr)
    }

  }

  def remainFiles() = {
    val fileList = dirs.flatMap(f => {
      //      val f = new File(x)
      f.listFiles().toList
    })
    if (fileList.size > 0) {
      logger.debug("remain files : " + fileList)
      true
    } else {
      false
    }

  }

}
